<?php
    get_header();
?>
<?php
                    global $wpdb;
                    $current_url = explode('?', cpx_current_url());
                    $url = $current_url[0];
                    $display = get_option('popular');
                    if($display == ''){
                        $display = 15;
                    }
                    if(isset($_GET['show_page']) && (int)$_GET['show_page']){
                        $page = $_GET['show_page'];
                    }
                            $my_query ="
                                SELECT SQL_CALC_FOUND_ROWS ID AS cp_id FROM {$wpdb->posts} 
                                INNER JOIN {$wpdb->postmeta} ON ({$wpdb->posts}.ID = {$wpdb->postmeta}.post_id) 
                                WHERE 1=1 AND {$wpdb->posts}.ID = {$wpdb->postmeta}.post_id AND {$wpdb->posts}.post_type = 'coupon' 
                                AND ({$wpdb->posts}.post_status = 'publish') AND {$wpdb->postmeta}.meta_key = 'store_coupon_metadata' 
                                GROUP BY {$wpdb->postmeta}.meta_value ORDER BY {$wpdb->posts}.post_date DESC
                                ";
                            $data = mysql_query($my_query);
                            $record = mysql_num_rows($data);
                            if($record > $display){
                                $pages = ceil($record/$display);
                            }else{
                                $pages = 1;
                            }
                            $start = (isset($_GET['show_page']) && (int)$_GET['show_page'] >= 0) ? (($page - 1)*$display) : 0 ;
                            $my_sql ="
                                SELECT SQL_CALC_FOUND_ROWS ID AS cp_id FROM {$wpdb->posts} 
                                INNER JOIN {$wpdb->postmeta} ON ({$wpdb->posts}.ID = {$wpdb->postmeta}.post_id) 
                                WHERE 1=1 AND {$wpdb->posts}.ID = {$wpdb->postmeta}.post_id AND {$wpdb->posts}.post_type = 'coupon' 
                                AND ({$wpdb->posts}.post_status = 'publish') AND {$wpdb->postmeta}.meta_key = 'store_coupon_metadata' 
                                GROUP BY {$wpdb->postmeta}.meta_value ORDER BY {$wpdb->posts}.post_date DESC LIMIT $start,$display
                            ";
                            $result = $wpdb->get_results($my_sql);
                            $total = count($result);
                ?>
        <div class="row-fluid">
            <div class="span8 content-sidebar">
                <div class="row-fluid boxf tax-store-category">
                     <div class="row-fluid header-coupon">
                         <div class="span12 code-coupon"><h2>Current Top <?php echo Popular;?> Coupons</h2></div>
                     </div>
                        <?php
                            foreach ($result as $v){
                                $cp_id = $v->cp_id;
                                cpx_get_coupon_detail_2($cp_id);
                            }
                        ?>
                        <?php 
                            
                            if($pages > 1 && $total > 0){
                                $current = ($start/$display)+1;
                                pagination_custom($url,$page,$pages,$current,3);
                            }
                        ?>
                    <?php 
                        if($total == 0){ ?>
                        <div class="row-fluid notfound">
                            <span>The coupon you need is not available in this category.</span>
                            <p>But you should see these coupons below or go.</p>
                        </div>
                        <?php
                            $page = get_option('pagination_404');
                            cpx_get_coupon_detail_3('','',$page);
                        }
                         ?>
               </div>
            </div>
            <div class="span4 right-sidebar">
                    <?php if(is_active_sidebar('cs-main-sidebar')) dynamic_sidebar( 'cs-main-sidebar' ); ?>
            </div>
        </div>
        <div class="row-fluid top-store-category">
               <?php if(is_active_sidebar('cs-text-link-sidebar')) dynamic_sidebar( 'cs-text-link-sidebar' ); ?>
        </div>
<?php get_footer(); ?>